﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace IronWinter
{
    /*
     *The objective has a square area around it, if the objective is not controlled it is contested. 
     *the objective is contested
     *If there is only one player in the Box then it is not contested and that player is the contyrolling players. 
     *
     * */

    public class Objectives : Terrain
    {
        protected bool contested;
        public Player controllingPlayer;
        int reqValue; 
        public Rectangle territory;
        protected TimeSpan lastResTick;

        AnimatedSprite sprite; //contested

        public Objectives(int x, int y, int width, int height, int value)
            : base(x, y, width, height, Game1.objective)
        {
            lastResTick = TimeSpan.MinValue;
            reqValue = value;

            controllingPlayer = null;

            int centerX = hitBox.Center.X;
            int centerY = hitBox.Center.Y;

            territory = new Rectangle(centerX - Map.cellSize * 5, centerY - Map.cellSize * 5, Map.cellSize * 10, Map.cellSize * 10);

            sprite = new AnimatedSprite(Game1.objective, 0, 0, width, height, 1);
        }
        /// <summary>
        /// Checks every unit of every player to see if is the objective hit box intersects with any units. If it intersects with units of only one player
        /// then the objective is not contested and the player is the controlling player of the objective. 
        /// otherwise there is no controlling player and the objective is contested. 
        /// </summary>

        public void Update(GameTime gameTime)
        {
            bool empty = true;
            contested = true;
            controllingPlayer = null;

            foreach (Player i in Game1.players)
            {
                foreach (Unit j in i.units)
                {
                    if (j.Bounds.Intersects(territory))
                    {
                        if (empty)
                        {
                            empty = false;
                            controllingPlayer = i;
                            contested = false;
                        }
                        else
                        {
                            contested = true;
                            controllingPlayer = null;
                        }
                        break; //Dont bother with This player anymore

                    }//ifBounds
                }//foreach Unit
            }//foreach Player

            if (controllingPlayer == null)
                foreach (Player p in Game1.players)
                    foreach (Building b in p.buildings)
                        if (b.hitBox.Intersects(territory))
                            controllingPlayer = p;

            //assignPointstoControllingPlayer
            if (controllingPlayer != null && gameTime.TotalGameTime >= Game1.RES_TICK + lastResTick)
            {
                lastResTick = gameTime.TotalGameTime;
                controllingPlayer.requisition += reqValue;
            }
        }// isContested


        // The value/Points this objective is worth
        public int value
        {
            get { return reqValue; }
        }

        public override void Draw(SpriteBatch sb)
        {
            if (controllingPlayer == null)
                aniSprite.tint = Color.White;
            else if (controllingPlayer.id == 1)
                aniSprite.tint = Game1.players[0].color;
            else if (controllingPlayer.id == 2)
                aniSprite.tint = Game1.players[1].color;
            else
                aniSprite.tint = Color.White;

            base.Draw(sb);
        }

    }
}
